﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace CompanyDic.SearchEngine
{
    public class PageParser
    {
        public static List<CompanySummary> SummaryList = new List<CompanySummary>();
        public static List<CompanyDetail> DetailList = new List<CompanyDetail>();
        private static List<string> SourceUrlList = new List<string>();

        public void ParseSummary()
        {
            UrlBuilder builder = new UrlBuilder();
            Dictionary<string, string> summariesUrls;
            while ((summariesUrls = builder.GetSummaryUrls()).Count > 0)
            {
                foreach (KeyValuePair<string, string> pair in summariesUrls)
                {
                    string html = WebHelper.GetHtml(pair.Key);
                    List<CompanySummary> summaries = WebHelper.HtmlAnalyzer(html, pair.Value);

                    if (summaries.Count == 0)
                    {
                        string city = pair.Value;
                        UrlBuilder.CityCurPageNoDic.Remove(city);
                        continue;
                    }

                    foreach (CompanySummary companySummary in summaries)
                    {
                        if (!SourceUrlList.Contains(companySummary.SourceUrl))
                        {
                            SourceUrlList.Add(companySummary.SourceUrl);
                            SummaryList.Add(companySummary);
                        }
                    }
                }
            }

            string path = "c:\a.txt";
            if(File.Exists(path))
                File.Delete(path);

            using (FileStream stream = new FileStream(path, FileMode.CreateNew, FileAccess.Write))
            {
                using (StreamWriter writer = new StreamWriter(stream))
                {
                    foreach (CompanySummary companySummary in SummaryList)
                    {
                        writer.WriteLine(string.Format("{0}:{1}", companySummary.Name, companySummary.SourceUrl));
                    }
                }
            }
        }
    }
}
